package com.song.controller.content;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.song.common.Result;
import com.song.controller.BaseController;
import com.song.entity.content.Content;
import com.song.service.content.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("${api.version}/content")
public class ContentController extends BaseController {

    @Autowired
    private ContentService contentService;

    /**
     *  根据某一主题查找对应所有的内容列表
     */
    @RequestMapping(value = "/detail/{topicId}")
    public String getContents(@PathVariable("topicId") String topicId, Model model) {
        EntityWrapper<Content> ew = new EntityWrapper<>();
        ew.eq("topic_id", topicId);
        List<Content> contents = contentService.selectList(ew);
        model.addAttribute("contents",contents);
        return "content/contentManage";
    }

    /**
     *  根据用户ID删除Bug
     */
    @RequestMapping("/deleteContentById")
    @ResponseBody
    public Result deleteContentById(int contentId) {
        boolean bool = contentService.deleteById(contentId);
        if (bool) {
            return success("删除内容成功！");
        } else {
            return error("删除内容失败！");
        }
    }
}
